<?php

App::uses('AppController', 'Controller');

/**
 * Images Controller
 *
 * @property Images $Arquivo
 */
class ImagesController extends AppController {

    public $name = 'Images';
    public $helpers = array('Html', 'Form', 'ImageSize');
    public $components = array('Upload', 'PImage');
    
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->deny('index');
        $this->Auth->deny('view');
    }

    public function redimensionar() {
        $dir = 'files' . DS . 'ovelha.jpg';
        //$this->ImageSize->crop($dir);
        $this->set('DIR', $dir);
        $this->set('HLP', $this->helpers);
    }

    public function resize($filename = null) {
        $dir = 'files' . DS;
        //if($this->PImage->resizeImage('crop', 'ovelha.jpg', $dir, 'ovelha.jpg', 70, 70));
        //resizeImage($cType = 'resize', $id, $imgFolder, $newName = false, $newWidth=false, $newHeight=false, $quality = 75, $bgcolor = false) 
        if ($this->PImage->resizeImage('resize', $filename, $dir, false, 300, 300, 750, false))
            ;
    }

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->Image->recursive = 0;
        $this->set('images', $this->paginate());
    }

    /**
     * view method
     *
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        $this->Image->id = $id;
        if (!$this->Image->exists()) {
            throw new NotFoundException(__('Invalid image'));
        }
        $this->set('image', $this->Image->read(null, $id));
    }

    public function add($data = null) {
        
            $this->request->data=$data;

            $this->Image->create();

            if (empty($this->request->data['Image']['Image']['tmp_name'])) {

                $this->Session->setFlash(__('É preciso enviar alguma imagem', true));

                return false;
            }

            $path = "files";

            $this->Upload->setPath($path);

            $new_image = $this->Upload->copyUploadedFile($this->request->data['Image']['Image'], '');



//grava dados do arquivo no banco de dados

            $this->request->data['Image']['dir'] = 'files';

            $this->request->data['Image']['file_name'] = $new_image;

            $this->request->data['Image']['file_size'] = number_format($this->request->data['Image']['Image']['size'] / 1024, 2) . " KB";

            $this->request->data['Image']['type'] = $this->request->data['Image']['Image']['type'];

            $this->resize($new_image);

            if ($this->Image->save($this->request->data)) { //salva a imagem
                $this->Session->setFlash(__('Imagem enviada com sucesso.', true));

                //$this->redirect(array('action' => 'index'));
            } else {

                $this->Session->setFlash('Desculpe. A imagem não pode ser salva. Tente novamente.', 'default', array('class' => 'errorMsg'));
                return false;
            }
        //} //fecha if - formulario enviado
        return $this->Image->find('first', array('order' => array('Image.created DESC')));
    }

    /**
     * edit method
     *
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        $this->Image->id = $id;
        if (!$this->Image->exists()) {
            throw new NotFoundException(__('Invalid image'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Image->save($this->request->data)) {
                $this->Session->setFlash(__('The image has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The image could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->Image->read(null, $id);
        }
    }

    /**
     * delete method
     *
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->request->data = $this->Image->findById($id);
        if (!$this->Image->exists()) {
            throw new NotFoundException(__('Invalid image'));
        }
        if ($this->Image->delete()) {
            $this->Session->setFlash(__('Image deleted'));
          //  $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Image was not deleted'));
       // $this->redirect(array('action' => 'index'));
    }

}
